/**
 * Copyright (c) 2016-2019 人人开源 All rights reserved.
 * <p>
 * https://www.renren.io
 * <p>
 * 版权所有，侵权必究！
 */

package io.renren.controller;

import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.entity.SysDictEntity;
import io.renren.param.SysDictQueryParam;
import io.renren.service.SysDictService;
import io.renren.vo.CityVo;
import io.renren.vo.ProvinceVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典
 *
 * @author Mark sunlightcs@gmail.com
 */
@RestController
@RequestMapping("sys/dict")
public class SysDictController {
    @Autowired
    private SysDictService sysDictService;


    /**
     * 信息
     */
    @RequestMapping("/info/{name}/{code}")
    public R info(@PathVariable("code") String code, @PathVariable("name") String name) {
        SysDictQueryParam param = new SysDictQueryParam();
        param.setCode(code);
        param.setName(name);
        SysDictEntity dict = sysDictService.queryByNameCode(param);

        return R.ok().put("page", dict);
    }

    /**
     * province & city
     */
    @RequestMapping("/info/provCity")
    public R provCity(@RequestBody Map<String, String> map) {


        SysDictQueryParam sysDictQueryParam = new SysDictQueryParam();
        sysDictQueryParam.setName("province");
        sysDictQueryParam.setType(map.get("compId"));
        List<SysDictEntity> provinces = sysDictService.queryByNameType(sysDictQueryParam);

        List<ProvinceVo> provinceVos = new ArrayList<>();
        for (SysDictEntity sysDictEntity : provinces) {
            Map<String, Object> cityParams = new HashMap<>();
            ProvinceVo provinceVo = ProvinceVo.convertFor(sysDictEntity);
            cityParams.put("name", provinceVo.getCode());

            PageUtils cityPage = sysDictService.queryPage(cityParams);
            List<CityVo> cityVos = new ArrayList<>();
            for (SysDictEntity cityEntity : (List<SysDictEntity>) cityPage.getList()) {
                CityVo cityVo = CityVo.convertFor(cityEntity);
                cityVos.add(cityVo);
            }
            provinceVo.setCities(cityVos);
            provinceVos.add(provinceVo);
        }


        return R.ok().put("page", new PageUtils(provinceVos, provinceVos.size(), 10, 1));
    }

    /**
     * 办公区
     */
    @RequestMapping("/info/floor")
    public R floor(@RequestBody Map<String, String> map) {


        SysDictQueryParam sysDictQueryParam = new SysDictQueryParam();
        sysDictQueryParam.setName("floor");
        sysDictQueryParam.setType(map.get("compId"));
        List<SysDictEntity> floors = sysDictService.queryByNameType(sysDictQueryParam);

        return R.ok().put("dict", new PageUtils(floors, floors.size(), 10, 1));
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{name}")
    public R info(@PathVariable("name") String name) {
        Map<String, Object> params = new HashMap<>();
        params.put("name", name);
        PageUtils page = sysDictService.queryPage(params);

        return R.ok().put("dict", page);
    }
}
